﻿using UnityEngine;

namespace Common
{
    [RequireComponent(typeof(Camera))]
    public class CameraObserve : MonoBehaviour
    {
        public bool CanObserve = true;

        private float moveSpeed = 3.0f;
        private float scrollSpeed = 2.5f;
        private float rotateSpeed = 100f;

        //物体的旋转角度
        private float x = 0.0f;
        private float y = 0.0f;

        private void Update()
        {
            if (!this.CanObserve)
            {
                return;
            }

            if (Input.GetAxis("Mouse ScrollWheel") != 0.0f && MainPanelMng.Instance.IsStickMode == false)
            {
                this.transform.position -= Input.GetAxis("Mouse ScrollWheel") * -this.transform.forward * this.scrollSpeed;
            }

            if (Input.GetMouseButton(1))
            {
                //DrawRectangleMng.Instance.SelectBoxMng.gameObject.SetActive(false);
                //DrawRectangleMng.Instance.Iron.SetActive(false);
                if (DrawRectangleMng.Instance.IsStickMode)
                {
                    DrawRectangleMng.Instance.Iron.transform.forward = this.transform.forward;
                    DrawRectangleMng.Instance.FlashIronBySelectBox();
                }
                
                //DrawRectangleMng.Instance.FlashIronBySelectBox();
                //鼠标沿x轴的移动乘以系数当作物体绕y轴的旋转角度。
                //鼠标沿y轴的移动乘以系数当作物体绕x轴的旋转角度。
                this.transform.RotateAround(this.transform.position, Vector3.up, Input.GetAxis("Mouse X") * this.rotateSpeed * Time.deltaTime);
                this.transform.RotateAround(this.transform.position, this.transform.right, -Input.GetAxis("Mouse Y") * this.rotateSpeed * Time.deltaTime);
            }

            if (Input.GetMouseButton(1) && Input.GetKey(KeyCode.W) && MainPanelMng.Instance.IsStickMode == false)
            {
                this.transform.position += this.transform.forward * Time.deltaTime * this.moveSpeed;
            }
            if (Input.GetMouseButton(1) && Input.GetKey(KeyCode.A))
            {
                this.transform.position += -this.transform.right * Time.deltaTime * this.moveSpeed;
            }
            if (Input.GetMouseButton(1) && Input.GetKey(KeyCode.S) && MainPanelMng.Instance.IsStickMode == false)
            {
                this.transform.position += -this.transform.forward * Time.deltaTime * this.moveSpeed;
            }
            if (Input.GetMouseButton(1) && Input.GetKey(KeyCode.D))
            {
                this.transform.position += this.transform.right * Time.deltaTime * this.moveSpeed;
            }
            if (Input.GetMouseButton(1) && Input.GetKey(KeyCode.Q))
            {
                this.transform.position += this.transform.up * Time.deltaTime * this.moveSpeed;
            }
            if (Input.GetMouseButton(1) && Input.GetKey(KeyCode.E))
            {
                this.transform.position += -this.transform.up * Time.deltaTime * this.moveSpeed;
            }
        }

        public void ChangeCameraProjection(bool isStickMode)
        {
            if (isStickMode)
            {
                Camera.main.orthographic = true;
                Camera.main.nearClipPlane = -200;
            }
            else
            {
                Camera.main.orthographic = false;
                Camera.main.nearClipPlane = 0.3f;
            }
        }
    }
}
